//
//  AliefuGymVC.m
//  doubleEduStudents
//
//  Created by 盛洋 on 2021/1/15.
//  Copyright © 2021 shengYang. All rights reserved.
//

#import "AliefuGymVC.h"
#import "SignUpVC.h"
#import "signUpCollectionCell.h"
#import "SignUPInforVC.h"
#import "AliefuTuijianCollectionCell.h"
#import "SearchVC.h"

@interface AliefuGymVC ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property (strong, nonatomic) IBOutlet UIView *back_view;

@property (strong, nonatomic) IBOutlet UIButton *left_btn1;
@property (strong, nonatomic) IBOutlet UIButton *left_btn2;

@property (strong, nonatomic) IBOutlet SYButton *right_btn1;
@property (strong, nonatomic) IBOutlet SYButton *right_btn2;
@property (strong, nonatomic) IBOutlet SYButton *right_btn3;
@property (strong, nonatomic) IBOutlet SYButton *right_btn4;
@property (strong, nonatomic) IBOutlet SYButton *right_btn5;
@property (strong, nonatomic) IBOutlet UICollectionView *collection_view;
@property (strong, nonatomic) IBOutlet UILabel *tuiJIan_lab;
@property (strong, nonatomic) IBOutlet UICollectionView *bottom_collectionView;


@end

@implementation AliefuGymVC
{
    UIView *line1_view;
    UIView *line2_view;
    UIView *line3_view;
    UIView *line4_view;
    UIView *line5_view;
    RequestServices *service;
    NSInteger page_index;//页码
    NSMutableArray *list_arr;//列表数组
    NSMutableArray *bottomArr;
    NSMutableDictionary *typeDic;
    NSMutableDictionary *statusDic;
    NSMutableArray *photoArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = self.title;
    if ([self.title isEqualToString:@"阿列夫赛道"]) {
        [self createNavigationBarRightBarButtonItemWithTitle:@"" font:16 image:@"aliefu_search" target:self action:@selector(search)];
    }
    [self.backKuang setHidden:YES];
    
    //设置头部btn选择
    [self setChooseBtn];
    //设置view
    [self setupUI];
    
    typeDic = [NSMutableDictionary dictionary];
    [typeDic setValue:@"1" forKey:@"type"];//默认赛事
    statusDic = [NSMutableDictionary dictionary];
    [statusDic setValue:@"-1" forKey:@"status"];//默认全部
    list_arr = [NSMutableArray array];
    
    page_index = 1;
    //请求
    [self loadRequest];
}



//设置btn
-(void)setChooseBtn{
    self.left_btn1.titleLabel.font = self.left_btn2.titleLabel.font = AutoLabFont2;
    
    [self.left_btn1 setBackgroundImage:[UIImage imageNamed:@"aliefu_titleBtn2_2"] forState:UIControlStateNormal];
    [self.left_btn2 setBackgroundImage:[UIImage imageNamed:@"aliefu_titlebtn1_1"] forState:UIControlStateNormal];
    
    [self.left_btn1 addTarget:self action:@selector(chooseSystem) forControlEvents:UIControlEventTouchUpInside];
    [self.left_btn2 addTarget:self action:@selector(chooseActivity) forControlEvents:UIControlEventTouchUpInside];
    
    [self.right_btn1 addTarget:self action:@selector(chooseAll) forControlEvents:UIControlEventTouchUpInside];
    line1_view = [[UIView alloc]init];
    line1_view.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:line1_view];
    [line1_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.right_btn1.mas_bottom).offset(5);
        make.centerX.equalTo(self.right_btn1.mas_centerX);
        make.height.equalTo(@(1));
        make.width.equalTo(@50);
    }];
    
    [self.right_btn2 addTarget:self action:@selector(chooseUnStart) forControlEvents:UIControlEventTouchUpInside];
    line2_view = [[UIView alloc]init];
    line2_view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:line2_view];
    [line2_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.right_btn2.mas_bottom).offset(5);
        make.centerX.equalTo(self.right_btn2.mas_centerX);
        make.height.equalTo(@(1));
        make.width.equalTo(@50);
    }];
    
    [self.right_btn3 addTarget:self action:@selector(chooseSign) forControlEvents:UIControlEventTouchUpInside];
    line3_view = [[UIView alloc]init];
    line3_view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:line3_view];
    [line3_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.right_btn3.mas_bottom).offset(5);
        make.centerX.equalTo(self.right_btn3.mas_centerX);
        make.height.equalTo(@(1));
        make.width.equalTo(@50);
    }];
    
    [self.right_btn4 addTarget:self action:@selector(choosePraise) forControlEvents:UIControlEventTouchUpInside];
    line4_view = [[UIView alloc]init];
    line4_view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:line4_view];
    [line4_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.right_btn4.mas_bottom).offset(5);
        make.centerX.equalTo(self.right_btn4.mas_centerX);
        make.height.equalTo(@(1));
        make.width.equalTo(@50);
    }];
    
    [self.right_btn5 addTarget:self action:@selector(chooseEnd) forControlEvents:UIControlEventTouchUpInside];
    line5_view = [[UIView alloc]init];
    line5_view.backgroundColor = [UIColor clearColor];
    [self.view addSubview:line5_view];
    [line5_view mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.right_btn5.mas_bottom).offset(5);
        make.centerX.equalTo(self.right_btn5.mas_centerX);
        make.height.equalTo(@(1));
        make.width.equalTo(@50);
    }];
}

//设置view
-(void)setupUI{
    
    [self.collection_view registerNib:[UINib nibWithNibName:@"signUpCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"signUpCollectionCell"];
    [self.bottom_collectionView registerNib:[UINib nibWithNibName:@"AliefuTuijianCollectionCell" bundle:nil] forCellWithReuseIdentifier:@"AliefuTuijianCollectionCell"];
    self.bottom_collectionView.tag = 8888;
    self.collection_view.delegate = self.bottom_collectionView.delegate= self;
    self.collection_view.dataSource = self.bottom_collectionView.dataSource= self;
    self.collection_view.showsVerticalScrollIndicator = NO;//隐藏竖滚动条
}

-(void)loadRequest{
    service = [[RequestServices alloc]initWithDelegate:self];
    service.view = self.view;
    [self headerRefresh:[statusDic[@"status"] integerValue] type:[typeDic[@"type"] integerValue]];
    __weak __typeof(self)weakSelf = self;
    //左划
    self.collection_view.mj_trailer =[MJRefreshTrailer trailerWithRefreshingBlock:^{
        [weakSelf footerRefresh:[self->statusDic[@"status"] integerValue] type:[self->typeDic[@"type"] integerValue]];
    }];
    self.collection_view.mj_trailer.hidden = YES;
}

//默认加载全部（-1）、赛事（1）
- (void)headerRefresh:(NSInteger)status type:(NSInteger)type
{
    [list_arr removeAllObjects];
    [service requestAliefuGymList:page_index size:10 status:status type:type keyword:@"" tag:8000];
}

- (void)footerRefresh:(NSInteger)status type:(NSInteger)type
{
    page_index++;
    [service requestAliefuGymList:page_index size:10 status:status type:type keyword:@"" tag:8001];
}


#pragma mark - requestDelegate
-(void)getFinished:(NSDictionary *)obj tag:(long long)tag{
    [self HideLoading];
    [self.collection_view.mj_trailer endRefreshing];
    if (tag == 8000 || tag == 8001) {
        
        NSMutableArray *tempArr = [NSMutableArray arrayWithArray:obj[@"list"]];
        if (tag == 8000) {
            if ([tempArr CheckNSArrayData]) {
                list_arr = [AliefuModel mj_objectArrayWithKeyValuesArray:obj[@"list"]];
            }
            if (tempArr.count == 0) {
                [self ShowNoDataView:self.bottom_collectionView Orgin:0];
            }
        }else {
            [list_arr addObjectsFromArray:[AliefuModel mj_objectArrayWithKeyValuesArray:tempArr]];
        }
        
        [self.collection_view reloadData];
    }
}

-(void)getError:(NSError *)error tag:(long long)tag{
    [self HideLoading];
    [self ShowNewMsg:error.domain];
}



#pragma mark ----- collectionViewDataSource
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
    if (collectionView.tag == 8888) {
        return 3;
    }else{
        return list_arr.count;
    }
}

//每个item的大小
-(CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath{
    if (collectionView.tag == 8888) {
        return CGSizeMake(collectionView.frame.size.width /5, collectionView.frame.size.height * 0.65);
    }else{
        return CGSizeMake((SCREEN_W - 75)/3, collectionView.frame.size.height * 0.9);
    }
}

//cell
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    if (collectionView.tag == 8888) {
        AliefuTuijianCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"AliefuTuijianCollectionCell" forIndexPath:indexPath];
        return cell;
        
    }else{
        signUpCollectionCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"signUpCollectionCell" forIndexPath:indexPath];
        //点击参加按钮
        cell.join_btn.tag = indexPath.row + 1000;
        [cell.join_btn addTarget:self action:@selector(joinGym:) forControlEvents:UIControlEventTouchUpInside];
        cell.model = list_arr[indexPath.row];
        return cell;
    }
}

//列间距
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    return 20;
}
//行间距（同一行的cell的间距）
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    return 0;
}

//点击赛事 type = 1
-(void)chooseSystem{
    [self.left_btn1 setBackgroundImage:[UIImage imageNamed:@"aliefu_titleBtn2_2"] forState:UIControlStateNormal];
    [self.left_btn2 setBackgroundImage:[UIImage imageNamed:@"aliefu_titlebtn1_1"] forState:UIControlStateNormal];
    [typeDic setValue:@"1" forKey:@"type"];
    [self headerRefresh:[statusDic[@"status"] integerValue] type:[typeDic[@"type"] integerValue]];
}

//点击活动 type = 0
-(void)chooseActivity{
    [self.left_btn2 setBackgroundImage:[UIImage imageNamed:@"aliefu_titlebtn2"] forState:UIControlStateNormal];
    [self.left_btn1 setBackgroundImage:[UIImage imageNamed:@"aliefu_titleBtn1"] forState:UIControlStateNormal];
    [typeDic setValue:@"0" forKey:@"type"];
    [self headerRefresh:[statusDic[@"status"] integerValue] type:[typeDic[@"type"] integerValue]];
}

//点击全部 status = -1
-(void)chooseAll{
    line1_view.backgroundColor = [UIColor whiteColor];
    line2_view.backgroundColor = line3_view.backgroundColor = line4_view.backgroundColor = line5_view.backgroundColor = [UIColor clearColor];
    [statusDic setValue:@"-1" forKey:@"status"];
    [self headerRefresh:[statusDic[@"status"] integerValue] type:[typeDic[@"type"] integerValue]];
}

//点击未开始 status = 0
-(void)chooseUnStart{
    line2_view.backgroundColor = [UIColor whiteColor];
    line1_view.backgroundColor = line3_view.backgroundColor = line4_view.backgroundColor = line5_view.backgroundColor = [UIColor clearColor];
    [statusDic setValue:@"0" forKey:@"status"];
    [self headerRefresh:[statusDic[@"status"] integerValue] type:[typeDic[@"type"] integerValue]];
}

//点击报名中 status = 1
-(void)chooseSign{
    line3_view.backgroundColor = [UIColor whiteColor];
    line2_view.backgroundColor = line1_view.backgroundColor = line4_view.backgroundColor = line5_view.backgroundColor = [UIColor clearColor];
    [statusDic setValue:@"1" forKey:@"status"];
    [self headerRefresh:[statusDic[@"status"] integerValue] type:[typeDic[@"type"] integerValue]];
}
 
//点击评奖中 status = 2
-(void)choosePraise{
    line4_view.backgroundColor = [UIColor whiteColor];
    line2_view.backgroundColor = line3_view.backgroundColor = line1_view.backgroundColor = line5_view.backgroundColor = [UIColor clearColor];
    [statusDic setValue:@"2" forKey:@"status"];
    [self headerRefresh:[statusDic[@"status"] integerValue] type:[typeDic[@"type"] integerValue]];
}

//点击已结束 status = 3
-(void)chooseEnd{
    line5_view.backgroundColor = [UIColor whiteColor];
    line2_view.backgroundColor = line3_view.backgroundColor = line4_view.backgroundColor = line1_view.backgroundColor = [UIColor clearColor];
    [statusDic setValue:@"3" forKey:@"status"];
    [self headerRefresh:[statusDic[@"status"] integerValue] type:[typeDic[@"type"] integerValue]];
}

//参加比赛
-(void)joinGym:(UIButton *)sender{
    
    SignUPInforVC *vc = [[SignUPInforVC alloc]init];
    AliefuModel *model = list_arr[sender.tag - 1000];
//    vc.model = model;
    vc.typeID = model.id;
    [self PushToViewController:vc];
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    SignUPInforVC *vc = [[SignUPInforVC alloc]init];
    AliefuModel *model = list_arr[indexPath.item];
//    vc.model = model;
    vc.typeID = model.id;
    [self PushToViewController:vc];
}

//跳转搜索界面
-(void)search{
    SearchVC *vc = [[SearchVC alloc]init];
    [self PushToViewController:vc];
}

-(void)viewDidDisappear:(BOOL)animated{
    SYButton *btn = [[SYButton alloc]init];
    [btn setIsChoose:NO];
}



/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
